/*
 * AssemblerApplication1.asm
 *
 *  Created: 2/19/2014 5:47:15 PM
 *   Author: Luy8
 */ 


.ORG 0
 LDI R31, HIGH(RAMEND)
 OUT SPH, R31
 LDI R31, LOW (RAMEND)
 OUT SPL, R31

 LDI R17, 0X14; COUNTER=20
 
 LDI R18, 0X03
 LDI R19, 0X05
 LDI R20, LOW(0X0000); R20:R21
 LDI R21, HIGH(0X0000)
 LDI R23, LOW(0X0000); R23:R24
 LDI R24, HIGH(0X0000)
    LDI R26, 0
 
 LDI R16, 0X20
L1: MOV R31, R16
 PUSH R16
 INC R16
 DEC R17
 BRNE L1

 LDI R22, 0
 LDI R17, 0X15
L2: //LDI R30, 0
 LDI R25, 0
 LDI R27, 0
DEC R17     ; decrement counter R19
 CP R17, R22    ; check if all 20 numbers have been parsed with counter 
 BREQ GAMEOVER   

 POP R31
 
 MOV R15, R31
 
 ADD R25, R15
 
 JMP L3
 
 //DEC R19//new
 //BRNE L2
 //BREQ GAMEOVER

L3: SUB R25, R18
 BREQ SUM
 BRLO SUM
 BRPL L3

SUM:ADD R27, R15
 SUB R25, R26
 BRNE L4
 ADD R20, R15
 RJMP L4
 
L4: SUB R27, R19
 BREQ SUM2
 BRLO SUM2
 BRPL L4

SUM2: SUB R27, R26
   BRNE L2
   ADD R23, R15
 RJMP L2
 
GAMEOVER:



 RET
 